﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using HenIT.Data.SqlClient;

namespace EventScavenger
{
    public partial class EventScavengerDAL : GenericSQLServerDAL
    {
        #region MachineLog methods
        public MachineLogEntry GetMachineLogEntry(string machine, string logName)
        {
            MachineLogEntry machineLogEntry = new MachineLogEntry(machine, logName);
            string sql = "SelectMachineLogEntry";
            try
            {
                SqlParameter[] parameters = new SqlParameter[] 
                {
                    new SqlParameter("@Machine", machineLogEntry.Machine),
                    new SqlParameter("@LogName", machineLogEntry.LogName),
                };
                DataSet mleds = GetDataSet(sql, CommandType.StoredProcedure, parameters);
                if (mleds.Tables.Count > 0 && mleds.Tables[0].Rows.Count > 0)
                {
                    DataRow r = mleds.Tables[0].Rows[0];
                    machineLogEntry = new MachineLogEntry(machine, logName);
                    machineLogEntry.MachineLogID = (int)r["MachineLogID"];
                    machineLogEntry.CollectorId = (int)r["CollectorID"];
                    machineLogEntry.Collector = r["Collector"].ToString();
                    machineLogEntry.Enabled = (bool)r["Enabled"];
                    machineLogEntry.PollFrequency = (int)r["PollFrequency"];
                    machineLogEntry.MaxEntryCount = (int)r["MaxEntryCount"];
                    machineLogEntry.LastPollTime = (r["LastPollTime"] == DBNull.Value ? new DateTime(2000, 1, 1) : (DateTime)r["LastPollTime"]);
                    if (r["PollFilterID"] != DBNull.Value)
                    {
                        machineLogEntry.PollFilterID = (int)r["PollFilterID"];
                        machineLogEntry.PollingFilterName = r["PollingFilterName"].ToString();
                        if (r["PollFilterLastChanged"] != DBNull.Value)
                        {
                            machineLogEntry.PollFilterLastChanged = (DateTime)r["PollFilterLastChanged"];
                        }
                    }                    
                }
                else
                {
                    machineLogEntry.MachineLogID = -1;
                    machineLogEntry.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                lastError = ex.ToString();
            }
            return machineLogEntry;
        }
        #endregion

        #region Polling filter methods
        public PollFilter GetPollFilterByID(int pollFilterId)
        {
            PollFilter pollFilter = null;
            string sql = "SelectPollFilter";
            SqlParameter[] parameters = new SqlParameter[] 
                {
                    new SqlParameter("@PollFilterID", pollFilterId),
                };
            try
            {
                DataSet pfds = GetDataSet(sql, CommandType.StoredProcedure, parameters);
                if (pfds.Tables.Count > 0 && pfds.Tables[0].Rows.Count > 0)
                {
                    pollFilter = new PollFilter();
                    DataRow r = pfds.Tables[0].Rows[0];
                    pollFilter.PollFilterID = pollFilterId;
                    pollFilter.Name = r["Name"].ToString();
                    pollFilter.MessageContains = (r["MessageContains"] == DBNull.Value ? "" : r["MessageContains"].ToString());
                    pollFilter.ExcludeInfo = (bool)r["ExcludeInfo"];
                    pollFilter.ExcludeWarn = (bool)r["ExcludeWarn"];
                    pollFilter.ExcludeErr = (bool)r["ExcludeErr"];
                    pollFilter.ExcludeSucAud = (bool)r["ExcludeSucAud"];
                    pollFilter.ExcludeFailAud = (bool)r["ExcludeFailAud"];
                    pollFilter.LastChanged = (r["LastChanged"] == DBNull.Value ? null : (DateTime?)r["LastChanged"]);
                }
                if (pollFilter != null)
                {
                    sql = "SelectPollFilterEventIDsByPollFilterID";
                    SqlParameter[] parameters2 = new SqlParameter[] 
                    {
                        new SqlParameter("@PollFilterID", pollFilterId),
                    };
                    DataSet pfeidds = GetDataSet(sql, CommandType.StoredProcedure, parameters2);
                    if (pfeidds.Tables.Count > 0 && pfeidds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow r in pfeidds.Tables[0].Rows)
                        {
                            if ((bool)r["Exclude"])
                            {
                                pollFilter.ExcludeEventIDs.Add((int)r["EventID"]);
                            }
                            else
                            {
                                pollFilter.IncludeEventIDs.Add((int)r["EventID"]);
                            }
                        }
                    }
                    sql = "SelectPollFilterSourcesByPollFilterID";
                    SqlParameter[] parameters3 = new SqlParameter[] 
                    {
                        new SqlParameter("@PollFilterID", pollFilterId),
                    };
                    DataSet pfsds = GetDataSet(sql, CommandType.StoredProcedure, parameters3);
                    if (pfsds.Tables.Count > 0 && pfsds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow r in pfsds.Tables[0].Rows)
                        {
                            if ((bool)r["Exclude"])
                            {
                                pollFilter.ExcludeSources.Add(r["Source"].ToString());
                            }
                            else
                            {
                                pollFilter.IncludeSources.Add(r["Source"].ToString());
                            }
                        }
                    }
                }
                if (pollFilter.ExcludeEventIDs.Count > 0 ||
                    pollFilter.IncludeEventIDs.Count > 0 ||
                    pollFilter.ExcludeSources.Count > 0 ||
                    pollFilter.IncludeSources.Count > 0 ||
                    pollFilter.MessageContains.Length > 0)
                {
                    pollFilter.UseListFilters = true;
                }
                else
                    pollFilter.UseListFilters = false;
            }
            catch (Exception ex)
            {
                lastError = ex.ToString();
            }
            return pollFilter;
        }
        #endregion
    }
}
